package com.jie.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.reggie.entity.Category;
import com.jie.reggie.entity.Dish;
import com.jie.reggie.entity.Setmeal;
import com.jie.reggie.mapper.CategoryMapper;
import com.jie.reggie.service.CategoryService;
import com.jie.reggie.service.DishService;
import com.jie.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/02/14:37
 * @Description:
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService service;
    @Override
    public void remove(Long id) {
        //是否关联菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(dishLambdaQueryWrapper);
        //如果关联
        if(count>0){
            //抛出业务异常
            throw new RuntimeException("当前分类下关联了菜品，不能删除");
        }
        //是否关联套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = service.count(setmealLambdaQueryWrapper);
        //如果关联
        if(count1>0){
            //抛出业务异常
            throw new RuntimeException("当前分类下关联了套餐，不能删除");
        }
        super.removeById(id);
    }
}
